﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EnterpriseFund.Common;
using DevExpress.Web.Mvc;
using System.Data;

namespace EnterpriseFund.Controllers
{
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class TaxAuditPolicyAuditController : AdminBaseController
    {
        //
        // GET: /TaxAuditPolicyAudit/
        [AuthorityActionFilter("m_TaxAuditPolicyAudit", Order = 1)]
        [LogPageActionFilter("政策审核", Order = 2)]
        public ActionResult Index()
        {
            string strwhere = " FStatus='2' ";
            BLL.AllowanceClass allowanceClassBLL = new BLL.AllowanceClass();
            ViewData["AllowanceClassList"] = allowanceClassBLL.GetModelList("");
            List<Model.TaxAuditForm> taxAuditFormList = new List<Model.TaxAuditForm>();
            taxAuditFormList = GetTaxAuditFormListList(strwhere, 1);
            return View(taxAuditFormList);
        }

        //public ActionResult Save()
        //{
        //    string taxAuditId = Fetch.Post("hidFTaxAuditId");
        //    string type = Fetch.Post("type");
        //    try
        //    {
        //        Model.TaxAuditForm t = new Model.TaxAuditForm();
        //        switch (Fetch.Post("hidFStatus"))
        //        {
        //            case "open":
        //                t.FStatus = "3";
        //                t.FIsReturn = "N";
        //                break;
        //            case "Return":
        //                t.FStatus = "1";
        //                t.FIsReturn = "Y";
        //                break;
        //        }

        //        t.FTaxAuditId = taxAuditId;
        //        BLL.TaxAuditForm tBLL = new BLL.TaxAuditForm();
        //        tBLL.UpdateStatus(t);
        //        ViewData["jsAlert"] = "操作成功";
        //        if (Fetch.Post("hidFStatus") == "open")
        //        {
        //            WorkFlow("t_TaxAuditForm", "30", taxAuditId, "open", "");
        //        }
        //    }
        //    catch(Exception ex)
        //    {
        //        ViewData["jsAlert"] = ex.Message;
        //    }

        //    return TaxAuditPolicyAuditEdit(taxAuditId);
        //}
        public ActionResult Save(Model.TaxAuditForm tAF)
        {

            string FPolicyNote = Fetch.Post("FPolicyNote");
            string taxAuditId = Fetch.Post("hidFTaxAuditId");

            string type = Fetch.Post("type");
            string status = "";
            try
            {
                Model.TaxAuditForm t = new Model.TaxAuditForm();
                switch (Fetch.Post("hidFStatus"))
                {
                    case "open":
                        t.FStatus = "3";
                        t.FIsReturn = "N";
                        t.FPolicyNote = FPolicyNote;
                        status = "2";
                        break;
                    case "Return":
                        t.FStatus = "1";
                        t.FIsReturn = "Y";
                        t.FPolicyNote = FPolicyNote;
                        status = "0";
                        break;
                }

                t.FTaxAuditId = taxAuditId;
                BLL.TaxAuditForm tBLL = new BLL.TaxAuditForm();
                tBLL.UpdateStatus(t, status);
                ViewData["jsAlert"] = "操作成功";
                if (Fetch.Post("hidFStatus") == "open")
                {
                    WorkFlow("t_TaxAuditForm", "30", taxAuditId, "open", "");
                }
            }
            catch (Exception ex)
            {
                ViewData["jsAlert"] = ex.Message;
            }

            return TaxAuditPolicyAuditEdit(taxAuditId);
        }
        public ActionResult TaxAuditPolicyAuditListPartial()
        {
            int pageIndex = Request.Params["pageIndex"] == null ? 0 : int.Parse(Request.Params["pageIndex"].ToString());
            int pageCount = Request.Params["PageCount"] == null ? 0 : int.Parse(Request.Params["PageCount"].ToString());
            int pageRowSize = Request.Params["pageRowSize"] == null ? 0 : int.Parse(Request.Params["pageRowSize"].ToString());
            int pageRowCount = Request.Params["pageRowCount"] == null ? 0 : int.Parse(Request.Params["pageRowCount"].ToString());
            string callbackArg = DevExpressHelper.CallbackArgument;

            string command = Request.Params["command"] == null ? "" : Request.Params["command"].ToString();

            if (command == "PAGERONCLICK")
            {
                string[] args = callbackArg.Split(new char[1] { '|' });
                string arg = args[args.Count() - 1];

                switch (arg)
                {
                    case "PBN;":
                        {
                            //下一页
                            pageIndex = pageIndex + 2;
                            break;
                        }
                    case "PBP;":
                        {
                            //上一页
                            //pageIndex = pageIndex;
                            break;
                        }
                    case "PBL;":
                        {
                            //最后一页
                            pageIndex = pageCount;
                            break;
                        }
                    case "PBF;":
                        {
                            //第一页
                            pageIndex = 1;
                            break;
                        }
                    default:
                        {
                            //点击数字页码
                            string tempIndex = arg.Replace("PN", "");
                            tempIndex = tempIndex.Replace(";", "");
                            if (RegExp.IsNumeric(tempIndex))
                            {
                                pageIndex = int.Parse(tempIndex) + 1;
                            }
                            break;
                        }

                }
            }
            else
            {
                pageIndex = pageIndex + 1;
            }
            string strwhere = " 1=1 ";

            string FStatus = Request.Params["FStatus"];
            string NSRBM = Request.Params["NSRBMQ"];
            string FAllowanceClassNo = Request.Params["FAllowanceClassNo"];
            if (FStatus != "")
            {

                strwhere += "  and FStatus='" + FStatus + "'";

            }
            else {
                strwhere += "  and FStatus='2'";
            }
            if (NSRBM != "")
            {
                strwhere += " and (NSRBM like '%" + NSRBM + "%' or NSRMC like '%" + NSRBM + "%')";
            }

            if (FAllowanceClassNo != "")
            {
                strwhere += " and FAllowanceClassNo='" + FAllowanceClassNo + "'";
            }
            //string NSRMC = Request.Params["NSRMC"];
            //string NSRBM = Request.Params["NSRBM"];
            //string HY_DM = Request.Params["HY_DM"];
            //string FEnterprisePropNo = Request.Params["FEnterprisePropNo"];
            //string FFinancePropNo = Request.Params["FFinancePropNo"];
            //string IsNoFull = Request.Params["IsNoFull"];

            BLL.TaxAuditForm taxAuditFormBLL = new BLL.TaxAuditForm();
            List<Model.TaxAuditForm> taxAuditFormList = new List<Model.TaxAuditForm>();
            taxAuditFormList = GetTaxAuditFormListList(strwhere, pageIndex);
            return PartialView("TaxAuditPolicyAuditListPartial", taxAuditFormList);
        }

        /// <summary>
        /// 获取定义的工作时间数据
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <param name="pageIndex">第几页</param>
        /// <returns>查询出来的数据</returns>
        private List<Model.TaxAuditForm> GetTaxAuditFormListList(string strWhere, int pageIndex)
        {
            BLL.Common commonBll = new BLL.Common();
            BLL.TaxAuditForm taxAuditFormBLL = new BLL.TaxAuditForm();
            string fiedls = "FTaxAuditId";
            int total;
            DataSet ds = commonBll.GetListByPage("fund.[fn_TaxAuditForm](" + OperateSession.get(GlobalConstant.PARAM_SYSYEAR) + ")", fiedls, Common.GlobalConstant.CONSTRING_PAGESIZE, pageIndex, strWhere, "FTaxAuditId", out total);
            List<Model.TaxAuditForm> taxAuditFormList = taxAuditFormBLL.DataTableToList(ds.Tables[0]);
            List<Model.TaxAuditForm> taxAuditFormAllList = new List<Model.TaxAuditForm>();
            total = taxAuditFormBLL.GetRecordCount(strWhere, OperateSession.get(GlobalConstant.PARAM_SYSYEAR));
            int pageRowSize = Common.GlobalConstant.CONSTRING_PAGESIZE;
            for (int i = 0; i < total; i++)
            {
                taxAuditFormAllList.Add(null);
            }
            for (int i = 0; i < pageRowSize; i++)
            {
                if (taxAuditFormList.Count > i)
                {
                    taxAuditFormAllList[((pageIndex - 1) * pageRowSize) + i] = taxAuditFormList[i];
                }
            }

            return taxAuditFormAllList;
        }

        public ActionResult TaxAuditPolicyEvalListPartial()
        {
            return PartialView();
        }

        public string Return(string taxAuditId)
        {
            try
            {
                //Model.TaxAuditForm t = new Model.TaxAuditForm();
                //t.FStatus = "AR";
                //t.FTaxAuditId = taxAuditId;
                //BLL.TaxAuditForm tBLL = new BLL.TaxAuditForm();
                //tBLL.UpdateStatus(t);
                return "ok";
            }
            catch
            {
                return "error";
            }
        }

        public string IsReturn(string taxAuditId)
        {
            BLL.TaxAuditPolicyEval TaxAuditPolicyEvalBLL = new BLL.TaxAuditPolicyEval();
            List<Model.TaxAuditPolicyEval> list = TaxAuditPolicyEvalBLL.GetModelList(" a.FTaxAuditId='" + taxAuditId + "' and FIssueID is not null");
            if (list.Count == 0)
            {
                return "ok";
            }
            else
            {
                return "no";
            }
        }


        public ActionResult ShowData(string taxAuditId, string y)
        {
            BLL.TaxAuditAdjust taxAuditAdjustBLL = new BLL.TaxAuditAdjust();

            DataSet ds = taxAuditAdjustBLL.GetListForPolicy(taxAuditId);

            DataTable dt = new DataTable();

            BLL.TaxAuditPolicyEval b = new BLL.TaxAuditPolicyEval();

            List<Model.TaxAuditPolicyEval> tle = b.GetModelList(" a.FTaxAuditId='" + taxAuditId + "'");

            dt = SetSavePolicyData(ds.Tables[0], tle, y);

            ViewData["FTaxAuditId"] = taxAuditId;

            return View(dt);
        }

        public ActionResult TaxAuditPolicyAuditEdit(string taxAuditId)
        {
            BLL.TaxAuditForm taxAuditFormBLL = new BLL.TaxAuditForm();
            Model.TaxAuditForm tAF = taxAuditFormBLL.GetModel(taxAuditId);
            BLL.DJ_NSRXX DJ_NSRXXBLL = new BLL.DJ_NSRXX();

            Model.DJ_NSRXX DJ_NSRXXModel = DJ_NSRXXBLL.GetModel(tAF.NSRDZDAH, tAF.FYear.ToString());

            tAF.NSRMC = DJ_NSRXXModel.NSRMC;

            tAF.NSRBM = DJ_NSRXXModel.NSRBM;

            BLL.EnterpriseProp enterprisePropBLL = new BLL.EnterpriseProp();

            ViewData["EnterprisePropList"] = enterprisePropBLL.GetModelList("");

            BLL.FinanceProp financePropBLL = new BLL.FinanceProp();

            ViewData["FinancePropList"] = financePropBLL.GetSelectList(tAF.FNewEnterprisePropNo, tAF.FYear.ToString());

            ViewData["FYear"] = tAF.FYear.ToString();

            BLL.AllowanceClass allowanceClassBLL = new BLL.AllowanceClass();
            ViewData["AllowanceClassList"] = allowanceClassBLL.GetModelList("");

            return View("TaxAuditPolicyAuditEdit", tAF);
        }

        public DataTable SetSavePolicyData(DataTable dt, List<Model.TaxAuditPolicyEval> tl, string y)
        {
            dt.Columns["se"].DataType = typeof(decimal);
            dt.Columns.Add("FPolicyID");
            dt.Columns.Add("FAllowanceAmount", typeof(decimal));
            dt.Columns.Add("base", typeof(decimal));
            dt.Columns.Add("rateIn", typeof(decimal));
            dt.Columns.Add("rateOut", typeof(decimal));
            dt.Columns.Add("FIsEvalBase");
            dt.Columns.Add("cz");
            dt.Columns.Add("FPolicyName");
            dt.Columns.Add("FFinancePropName");
            dt.Columns.Add("scounts");

            BLL.SupportPolicy sBLL = new BLL.SupportPolicy();
            decimal sumAmount = 0;
            decimal sumSe = 0;
            foreach (Model.TaxAuditPolicyEval t in tl)
            {
                Model.SupportPolicy s = sBLL.GetModel(t.FPolicyID, decimal.Parse(y));
                t.FAllowanceAmount = t.FAllowanceAmount / 10000;
                DataRow[] dtrl = dt.Select("FTaxClassAllowanceNo='" + t.FTaxClassAllowanceNo + "'");

                dtrl[0]["se"] = decimal.Round(decimal.Parse(dtrl[0]["se"].ToString()), 0);

                dtrl[0]["FAllowanceAmount"] = (t.FAllowanceAmount == null ? decimal.Parse("0") : decimal.Parse(t.FAllowanceAmount.ToString()));

                dtrl[0]["base"] = (t.FBaseAmount == null ? decimal.Parse("0") : decimal.Round(decimal.Parse(t.FBaseAmount.ToString()), 0));

                dtrl[0]["rateIn"] = (t.FInnerRate == null ? decimal.Parse("0") : decimal.Round(decimal.Parse(t.FInnerRate.ToString()) * 100, 2));

                dtrl[0]["rateOut"] = (t.FOuterRate == null ? decimal.Parse("0") : decimal.Round(decimal.Parse(t.FOuterRate.ToString()) * 100, 2));

                if (s != null)
                {
                    dtrl[0]["FIsEvalBase"] = s.FIsEvalBase;
                    dtrl[0]["FPolicyName"] = s.FPolicyName;
                }

                dtrl[0]["FPolicyID"] = t.FPolicyID;


                dtrl[0]["cz"] = "mx";

                dtrl[0]["FFinancePropName"] = t.FFinancePropName;
                dtrl[0]["scounts"] = t.scounts;
                sumAmount += (t.FAllowanceAmount == null ? 0 : decimal.Parse(t.FAllowanceAmount.ToString()));

                sumSe += (dtrl[0]["se"] != null && dtrl[0]["se"].ToString() != "" ? decimal.Parse(dtrl[0]["se"].ToString()) : 0);
            }

            DataRow dr = dt.NewRow();

            dr["cz"] = "总计";

            dr["FAllowanceAmount"] = sumAmount;

            dr["se"] = decimal.Round(sumSe, 0);
            dr["scounts"] = 0;
            dt.Rows.Add(dr);

            return dt;
        }

    }
}
